<?php
namespace app\api\controller;

use app\api\controller\Base;
use think\Db;

class Square extends Base
{
    // 发布广场说
    public function shareSquare()
    {
        if (request()->isPost()) {
            $data = Request()->param(true);
            if (!$this->con_validate->scene('shareSquare')->check($data)) {
                return json_return(301, $this->con_validate->getError());
            }
            $data['remote_device'] = Request()->header('remote-device');
            if (!$data['remote_device']) {return json_return(301, '没有获取终端设备号');}
            list($begin, $end) = $this->timer->today();
            $count             = model('Square')->where(['user_id' => $data['user_id']])->where('mtime', 'between time', [$begin, $end])->count();
            if ($count > 10) {return json_return(302, '你今天已经发布了很多广场说了，请明天再来哟。');}
            // 启动事务
            Db::startTrans();
            try {
                $num = "z_square_" . $this->get_file_name(20);
                $res = model('Square')::create([
                    'user_id'      => $data['user_id'],
                    'square_num'   => $num,
                    'square_title' => $data['square_title'],
                    'lat'          => $data['lat'],
                    'lng'          => $data['lng'],
                    'country'      => $data['square_country'],
                    'province'     => $data['square_province'],
                    'city'         => $data['square_city'],
                    'district'     => $data['square_district'],
                    'mtime'        => date('Y-m-d H:i:s', time()),
                    'ctime'        => date('Y-m-d H:i:s', time()),
                ]);
                // halt($res->id);
                $id = $res->id;
                // 检验广场说图片
                if (isset($data['images'])) {
                    $rst  = $this->uploadsFiles($data['images'], 600, 400, '/uploads/user/square_photo/');
                    $list = [];
                    foreach ($rst as $key => $value) {
                        $list[] = ['square_id' => $id, 'img' => $value];
                    }
                    model('SquarePhoto')->saveAll($list);
                    $res['images'] = $rst;
                }
                // 提交事务
                Db::commit();
                return json_return(200, '发布广场说成功', ['info' => $res]);
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return json_return(304, '系统错误', $e->getMessage());
            }
        } else {
            return json_return(399, '非法请求');
        }

    }

    // 广场说图片
    public function photoSquare()
    {
        if (request()->isPost()) {
            $data = Request()->param();
            if (!$this->con_validate->scene('photoSquare')->check($data)) {
                return json_return(301, $this->con_validate->getError());
            }
            $data['remote_device'] = Request()->header('remote-device');
            if (!$data['remote_device']) {return json_return(301, '没有获取终端设备号');}
            $files = request()->file('images');
            if (!$files) {return json_return(302, '未获取上传图像');}
            $res  = $this->uploadsFiles($files, 600, 400, '/uploads/user/square_photo/');
            $list = [];
            foreach ($res as $key => $value) {
                $list[] = ['square_id' => $data['square_id'], 'img' => $value];
            }
            return model('SquarePhoto')->saveAll($list) ? json_return(200, '上传广场说图片成功') : json_return(303, '网络繁忙，请稍后再试');
        } else {
            return json_return(399, '非法请求');
        }

    }
}
